// pages/home/components/qualifications/qualifications.js
const http = require('../../../../app/app.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    infoList: [],
    infoIndex: -1,
    infoMess:{
      question: [],
      answer: []
    },
    problem: '',
    showtips: [],
    showtipsOne: '',
    
  },
  infoList (e) {
    const that = this;
    const ind = e.currentTarget.dataset.id;
    const problem_id = this.data.infoList[ind].option_abid;
    const answer_id = this.data.infoList[ind].option_id;
    const last_answer = this.data.infoList[ind].option_content;
    const last_question = this.data.problem;
    this.data.infoMess.question.forEach(function (item, index, arr) { // 过滤
      if (item == problem_id) {
        arr.splice(index);
        that.data.infoMess.answer.splice(index);
      }
    })
    this.setData({
      infoIndex: ind
    })
    if (problem_id) {
      const question = this.data.infoMess.question.concat(problem_id);
      const answer = this.data.infoMess.answer.concat(answer_id);
      this.data.infoMess.last_question = last_question;
      this.data.infoMess.last_answer = last_answer;
      this.data.infoMess.question = question;
      this.data.infoMess.answer = answer;
      wx.setStorageSync('infoMessage', this.data.infoMess)
      const infoList = { // 给后台要传的值
        question: problem_id,
        answer: answer_id
      }
      wx.request({// 判断是否进入答案页面
        url: http.selAnswer,
        method: 'POST',
        data: infoList,
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success(res) {
          // console.log(res)
          const sortnum = res.data.sortnum
          const answers = JSON.stringify(res.data.answerre.answer_content)
          if (res.data.re == 'succ') {
            wx.navigateTo({//跳转下一个页面
              url: '../qualifications_1/qualifications_1?sortnum=' + sortnum,
            })
          } else {
            wx.navigateTo({//跳转结果页面
              url: '../qualifications_2/qualifications_2?answers=' + answers,
            })
          }
        }
      })
    }
  },
  backBut (e) { // 返回首页
    wx.switchTab({
      url: '../../service/service',
    })
  },
  telBut () {
    wx.makePhoneCall({
      phoneNumber: '0531-8867688' //仅为示例，并非真实的电话号码
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '购房资质'
    })
    wx.showLoading({ // 加载数据前的loading
      title: '数据加载中...'
    })
    const that = this;
    wx.request({// 获取页面数据
      url: http.abilityOne,
      method: 'POST',
      data: {},
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success (res) {
        // console.log(res)
        wx.hideLoading() // 数据调出后关闭loading
        const result = res.data.optionre
        const problem = res.data.showre.ability_content
        const showtips = res.data.showre.showtips;
        const showtipsOne = res.data.showre.showtips.shift();
        that.setData({
          infoList: result,
          problem: problem,
          showtips: showtips,
          showtipsOne: showtipsOne
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})